package com.jbat.gwt.tiledmapcreator.client.places;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceTokenizer;

/**
 * Defines a place for editing a map. Each place must have a valid tileset and size. These
 * parameters are passed to the appropriate views by the various activities that are started when
 * the place is visited.
 */
public class MapDesignPlace extends Place {

  /**
   * Gets the tokens (from the URL) and parses them.
   */
  public static class Tokenizer implements PlaceTokenizer<MapDesignPlace> {
    @Override
    public MapDesignPlace getPlace(String token) {
      return new MapDesignPlace(token);
    }

    @Override
    public String getToken(MapDesignPlace place) {
      String tileSetName = place.getTileSetName();
      String rowString = String.valueOf(place.getRows());
      String colString = String.valueOf(place.getCols());
      return tileSetName + "&" + rowString + "&" + colString;
    }
  }

  private String tileSetName;
  private int rows;

  private int cols;

  public MapDesignPlace(String token) {
    String[] stringArray = token.split("&");
    this.tileSetName = stringArray[0];
    this.rows = Integer.parseInt(stringArray[1]);
    this.cols = Integer.parseInt(stringArray[2]);
  }

  public int getCols() {
    return cols;
  }

  public int getRows() {
    return rows;
  }

  public String getTileSetName() {
    return tileSetName;
  }
}
